using NUnit.Framework;
using PowerfulExtensions.Helpers;

namespace PowerfulExtensions.Test.ByMembersEqualityComparerTest
{
    [TestFixture]
    class EqualsFieldsTest
    {
        private MyClass _obj1;
        private MyClass _obj2;

        [SetUp]
        public void Setup()
        {
            _obj1 = new MyClass();
            _obj2 = new MyClass();
        }

        [Test]
        public void ObjectsWith_TheSame_UnaryField_Should_BeEqual()
        {
            _obj1.IntField = 1;
            _obj2.IntField = 1;
            var comparer = new ByMembersEqualityComparer<MyClass>(s => s.IntField);
            var result = comparer.Equals(_obj1, _obj2);

            Assert.AreEqual(true, result);
        }

        [Test]
        public void ObjectsWith_TheSame_MemberField_Should_BeEqual()
        {
            _obj1.StrField = "1";
            _obj2.StrField = "1";
            var comparer = new ByMembersEqualityComparer<MyClass>(s => s.StrField);
            var result = comparer.Equals(_obj1, _obj2);

            Assert.AreEqual(true, result);
        }

        [Test]
        public void ObjectsWith_Not_TheSame_UnaryField_Should_Not_BeEqual()
        {
            _obj1.IntField = 1;
            _obj2.IntField = 2;
            var comparer = new ByMembersEqualityComparer<MyClass>(s => s.IntField);
            var result = comparer.Equals(_obj1, _obj2);

            Assert.AreEqual(false, result);
        }

        [Test]
        public void ObjectsWith_NotTheSame_MemberField_Should_Not_BeEqual()
        {
            _obj1.StrField = "1";
            _obj2.StrField = "2";
            var comparer = new ByMembersEqualityComparer<MyClass>(s => s.StrField);
            var result = comparer.Equals(_obj1, _obj2);

            Assert.AreEqual(false, result);
        }

        [Test]
        public void TheSame_CompareChainedFields_Should_BeEqual()
        {
            _obj1.StrField = "1";
            _obj2.StrField = "2";
            var comparer = new ByMembersEqualityComparer<MyClass>(s => s.StrField.Length);
            var result = comparer.Equals(_obj1, _obj2);

            Assert.AreEqual(true, result);
        }

        [Test]
        public void Not_TheSame_CompareChainedFields_Should_Not_BeEqual()
        {
            _obj1.StrField = "1";
            _obj2.StrField = "21";
            var comparer = new ByMembersEqualityComparer<MyClass>(s => s.StrField.Length);
            var result = comparer.Equals(_obj1, _obj2);

            Assert.AreEqual(false, result);
        }
    }
}
